package com.egdigital.allzic.adswizz;

import java.util.HashMap;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.egdigital.allzic.R;
import com.egdigital.allzic.service.Scheduler;
import com.egdigital.allzic.service.Scheduler.Mode;
import com.egdigital.allzic.service.Scheduler.OnScheduleTimeListener;
import com.egdigital.allzic.utils.ImageManager;
import com.egdigital.allzic.utils.PrefManager;
import com.egdigital.allzic.utils.Utils;

public class AdsWizzManager {

	private static final String		TAG	= AdsWizzManager.class.getSimpleName(); // log
	private XMLDownloader			mDownloader;
	private final ImageManager		mImageManager;
	private static AdsWizzManager	mAdsWizzManagerInstance;

	private AdsWizzManager(Context context) {
		super();
		mImageManager = new ImageManager(context);
	}

	public static AdsWizzManager getInstance(Context context) {
		if (mAdsWizzManagerInstance == null) {
			mAdsWizzManagerInstance = new AdsWizzManager(context);
		}
		return mAdsWizzManagerInstance;
	}

	public void setupAdsWizz(final Context context, final ImageView view) {
		mDownloader = new XMLDownloader(context.getString(R.string.adswizz_request), new OnXMLDownloadListener() {

			@Override
			public void OnDownloadFinish(final HashMap<String, String> data) {
				if (data != null) {
					// preference save
					final PrefManager preferenceManager = PrefManager.getInstance(context);
					try {
						preferenceManager.addStringMap_prefs(data);
					} catch (Exception e) {
					}
					if (preferenceManager.getString_prefForKey(XMLHandler.COMPANION_ADS_TYPE).equals(XMLHandler.COMPANION_ADS_BANNER)) {
						try {
							Utils.setImageFromUrl(mImageManager, preferenceManager.getString_prefForKey(XMLHandler.COMPANION_IMAGE), view);
						} catch (Exception e) {
						}
					} else if (preferenceManager.getString_prefForKey(XMLHandler.COMPANION_ADS_TYPE).equals(XMLHandler.COMPANION_ADS_INTERSTITIAL)) {
						openAdsWizzInterstitial(preferenceManager.getString_prefForKey(XMLHandler.COMPANION_IMAGE), preferenceManager.getString_prefForKey(XMLHandler.COMPANION_CLICK_THROUGHT), context);
					}
				}
			}

			@Override
			public void OnDownloadNoNetwork() {
			}

			@Override
			public void OnDownloadFailed() {
			}
		});
		mDownloader.execute(context);
	}

	private void openAdsWizzInterstitial(String urlImage, final String urlClick, final Context context) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.view_adswizz_interstitial, null);
		RelativeLayout layout = (RelativeLayout) view.findViewById(R.id.adswizz_pub);
		ImageView image = (ImageView) view.findViewById(R.id.adswizz_image);
		final ImageButton button = (ImageButton) view.findViewById(R.id.adswizz_close);
		final TextView textCount = (TextView) view.findViewById(R.id.adswizz_count);
		//
		final Dialog dialog = new Dialog(context);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(layout);
		dialog.getWindow().setBackgroundDrawable(new ColorDrawable(R.color.clr_trans_dark_grey));
		dialog.setCanceledOnTouchOutside(false); // Gingerbread support
		dialog.setCancelable(false);
		//
		Window window = dialog.getWindow();
		WindowManager.LayoutParams windowLayoutParams = window.getAttributes();
		windowLayoutParams.gravity = Gravity.CENTER;
		window.setAttributes(windowLayoutParams);
		dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		//
		// count schedule
		//
		PrefManager preferenceManager = PrefManager.getInstance(context);
		int duration = preferenceManager.getInt_prefForKey(XMLHandler.MEDIA_FILE_DURATION);
		final Scheduler scheduler = new Scheduler(1000, 5, Mode.DESC);
		scheduler.setOnScheduleTimeListener(new OnScheduleTimeListener() {

			@Override
			public void onScheduleTime(int count) {
				textCount.setText(count + " " + context.getString(R.string.adswizz_count));
				if (count == 0) {
					dialog.dismiss();
				}
			}
		});
		scheduler.startTimer();
		//
		layout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				adswizzClickIntent(urlClick, context);
			}
		});
		try {
			Utils.setImageFromUrl(mImageManager, preferenceManager.getString_prefForKey(XMLHandler.COMPANION_IMAGE), image);
		} catch (Exception e) {
		}
		try {
			dialog.show();
		} catch (Exception e) {
		}
	}

	private boolean adswizzClickIntent(String url, Context context) {
		if (!url.startsWith("http://") && !url.startsWith("https://")) {
			url += "http://";
		}
		try {
			Intent intent = new Intent(Intent.ACTION_VIEW);
			intent.setData(Uri.parse(url));
			context.startActivity(intent);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
}
